/**
 * Created by shenhai on 15/9/1.
 */


var menuService = require('../../../services/mis/system/menuService');
var messageUtil = require('../../../common/utils/messageUtil');
var toolUtil = require('../../../common/utils/toolUtil');


exports.menuList = function(req, res) {

    menuService.menuListService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when menuList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when menuList...'));
        }else {
            var data = {
                count:results[0]["count"],
                totalPage: results[0]["totalPage"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};



exports.menuAllList = function(req, res) {

    menuService.menuAllListService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when menuAllList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when menuAllList...'));
        }else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }

    });
};


exports.menuAdd = function(req, res) {

    menuService.menuAddService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when menuAdd...'));
        }else {

            menuService.allMenuListService(req, function(err, results) {
                if (err) {
                    res.json(messageUtil.composeFailedMessage(req, 'Exception happen when menuAdd...'));
                }else {
                    res.json(messageUtil.composeSucceededMessage(req, null, results));
                }
            });

        }
    });
};


exports.menuDelete = function(req, res) {

    menuService.menuDeleteService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when menuDelete...'));
        }else {
            menuService.allMenuListService(req, function(err, results) {
                if (err) {
                    res.json(messageUtil.composeFailedMessage(req, 'Exception happen when menuDelete...'));
                }else {
                    res.json(messageUtil.composeSucceededMessage(req, null, results));
                }
            });
        }

    });
};

exports.menuEdit = function(req, res) {

    menuService.menuEditService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when menuEdit...'));
        }else {
            menuService.allMenuListService(req, function(err, results) {
                if (err) {
                    res.json(messageUtil.composeFailedMessage(req, 'Exception happen when menuEdit...'));
                }else {
                    res.json(messageUtil.composeSucceededMessage(req, null, results));
                }
            });
        }
    });
};


